<template>
	<view class="Receipt">
		<view class="container">
			<view class="tag" v-show="entity.IsCancel">作废</view>
			<view class="h3">{{ entity.CompanyName }}</view>
			<view class="cell" style="display: flex;justify-content: space-between;">
				<!-- 只有赊账条件显示B 其余条件显示S -->
				<text v-if="entity.OrderType==1">单号:{{getReplaceId('B'+entity.Id) }}</text>
				<text v-else>单号:{{entity.Id?getReplaceId('S'+entity.Id):"" }}</text>
				<view class="subscript" v-if="mainState"
					style="display: flex;flex-direction: column;align-items: center;">
					<uni-tag v-if="entity.Tag==='已确认'" style="margin-bottom: 10rpx;" text="差异已确认" type="primary" />
					<image style="width: 100rpx;height: 100rpx;" src="../../static/chayi.png" mode=""></image>
				</view>
			</view>
			<view class="cell">
				<text>时间:</text>{{ entity.CreateTime?entity.CreateTime.substring(0, 19):'' }}
			</view>
			<view v-if="entity.OrderType == 1" class="cell"><text>账期:</text>{{ entity.Period + ' 天 '||'' }}</view>
			<view class="cell"><text>客户:</text>{{ entity.cCusName||'散客' }}</view>
			<view class="cell" style="display: flex;">
				<text>客户地址:</text>
				<text style="flex: 7;">{{ entity.cCusAddress||'' }}</text>
			</view>
			<view class="cell"><text>客户电话:</text>{{ entity.cCusPhone||'' }}</view>
			<view class="" style="display: flex;justify-content: space-between;">
				<!-- <view class="cell"><text>业务员:</text>{{ entity.RealName||'' }}</view> -->
				<view class="edit_view">
					<button :disabled="disabled" class="btn_add" type="primary" v-show="!btnState" @click="add"
						style="">
						{{disabled==false?'添加商品':isGet?'回执单生成中':'生成中...'}}
					</button>
				</view>
			</view>

			<view v-show="!btnState">
				<!--  -->
				<view class="btn_view" style="">
					<button :disabled="disabled" class="btn_edit" type="default" @click="cancel" v-show="!btnState">
						取消
					</button>
					<button :disabled="disabled" class="btn_submit" type="primary" @click="submit" v-show="!btnState"
						:style="{width:disabled==true?'150px':'100px'}">
						{{disabled==false?'确认':isGet?'回执单生成中':'生成中...'}}
					</button>
				</view>
			</view>
			<view class="tableRow" style="border-bottom: 1px dashed #ccc; margin-top: 10px; line-height: 2">
				<text class="tableIndex"></text>
				<text class="tableCol left">品名/编码</text>
				<text class="tableCol">数量</text>
				<!-- <text class="tableCol">单位</text> -->
				<text class="tableCol">单价</text>
				<text class="tableCol">金额</text>
			</view>
			<view style="padding: 10px 0">
				<view v-for="(item, index) in data" :key="index">
					<view class="tableRow">
						<text v-show="btnState" class="tableIndex">{{ index + 1 }}</text>
						<uni-icons @click="del(index)" v-show="!btnState" type="trash" size="30"
							style="color: red;"></uni-icons>
						<text class="tableCol left">{{ item.InventName }}</text>
						<text class="tableCol" v-show="btnState">{{ item.Quantity }}</text>
						<input class="tableInp" @blur="changeVal(item)" type="digit" v-model="item.Quantity"
							v-show="!btnState" style="text-align: center;" />
						<!-- <text class="tableCol">{{ item.GroupName }}</text> -->
						<text class="tableCol" v-show="btnState">{{ momenyFormat(item.Price) }}</text>
						<input class="tableInp" @blur="changeVal(item)" type="digit" v-model="item.Price"
							v-show="!btnState" @focus="setNull(index)" />
						<text class="tableCol">{{ momenyFormat(item.Money) }}</text>
					</view>
					<view class="tableRow">
						<text class="tableIndex"></text>
						<text class="tableCol left">{{ item.cInvCode }}</text>
						<text class="tableCol">{{ item.GroupName }}</text>
						<text v-if="item.cInvStd" class="tableCol col2">规格: {{ item.cInvStd }}</text>
						<text v-else class="tableCol col2"></text>
						<!-- <text class="tableCol">{{ item.GroupName }}</text> -->
					</view>
				</view>
			</view>

			<view class="tableRow" style="border-top: 1px dashed #ccc; line-height: 2; margin-bottom: 10px">
				<text class="tableIndex"></text>
				<text class="tableCol left">合计</text>
				<text class="tableCol">{{ entity.TotalQuantity }}</text>
				<text class="tableCol"></text>
				<text class="tableCol" style="">{{ momenyFormat(entity.TotalSum) }}</text>
			</view>
			<view class="cell">
				<text>{{entity.PaymentType}}:</text>
				<!-- {{entity.Type == 0?momenyFormat(entity.ReceiveSum):momenyFormat(entity.PendingSum)}} -->
				{{momenyFormat(entity.TotalSum)}}
				<!-- {{entity.PendingSum}} -->
			</view>
			<view v-if="xpqrcode" style="text-align: center; padding-top: 50px;padding-bottom: 60px;">
				<text>{{this.ReceiptPhrase}}</text>
				<tki-qrcode style="margin-top: 40rpx;" ref="qrcode" :val="xpqrcode" :size="300" unit="upx" />
				<text>扫码支付</text>
				<view class="txt">{{entity.Address}}</view>
				<view class="txt">签收家提供技术支持</view>
			</view>
		</view>
		<view v-if="Sign" style="background-color: #fff;padding: 20rpx;box-sizing: border-box;font-weight: bold;">
			客户签字
		</view>
		<view v-if="Sign" class="imgBox">
			<img class="img" :src="Sign" alt="客户签名">
		</view>
		<!-- 多项操作 -->
		<uni-popup ref="share" type="share" safeArea backgroundColor="#fff">
			<uni-popup-share title="操作" :bottom-data="bottomData" @select="selectShare"></uni-popup-share>
		</uni-popup>
		<!-- 短信发送 -->
		<uni-popup ref="popup" type="dialog">
			<uni-popup-dialog mode="input" message="成功消息" title="短信分享" :duration="2000" :before-close="true"
				@close="close" @confirm="confirm" placeholder="请输入客户手机号"
				:value="entity.cCusPhone=='暂无电话'?'':entity.cCusPhone"></uni-popup-dialog>
		</uni-popup>
		<!-- 小票短语 -->
		<!-- <uni-popup ref="ReceiptPhrasePopup" type="dialog">
			<uni-popup-dialog mode="input" message="成功消息" title="小票短语" :duration="2000" :before-close="true"
				@close="ReceiptPhraseClose" @confirm="ReceiptPhraseConfirm" :value="ReceiptPhrase"
				placeholder="请输入小票短语"></uni-popup-dialog>
		</uni-popup> -->
		<bleConnect ref="bleConnect" :Sign="Sign" :printData="entity" :data="data" v-show="false" />
	</view>
</template>

<script>
	// 引入底部menu 数据
	import {
		bottomShareData,
		bottomSinDataQian,
		bottomCopyData,
		bottomNoneEditTagData,
		bottomDataThree,
		bottomDataThreeNoneTag,
		bottomDataShezhangQian,
		bottomDataShezhangFenxiang,
		bottomDataShezhangAll,
		bottomDataShezhangChayi,
		bottomDataShezhangQueren,
		bottomDataShezhangNoneTag
	} from './bottom.js'
	import bleConnect from './bleConnect.vue'
	import {
		regPhone
	} from '@/utils/regExp.js'
	import {
		momenyFormat
	} from '@/util/util.js'
	import tkiQrcode from "@/components/tki-qrcode/tki-qrcode.vue"
	export default {
		components: {
			tkiQrcode,
			bleConnect
		},
		data() {
			return {
				isGet: "",
				disabled: false,
				tagState: false,
				btnState: true,
				Id: "",
				billId: "",
				entity: {},
				data: [],
				billList: [],
				iprint: '',
				// 显示签收的底部menu
				bottomShareData: bottomShareData,
				newBottomData: [{
						text: '编辑',
						icon: '/static/share/editbill.png',
						name: 'shanchu'
					},
					{
						text: '复制',
						icon: '/static/share/copybill.png',
						name: 'shanchu'
					},

					{
						text: '打印',
						icon: '/static/share/dayin.png',
						name: 'dayin'
					},
					{
						text: '微信分享',
						icon: '/static/share/wx.png',
						name: 'wxfenxiang'
					},
					{
						text: '短信分享',
						icon: '/static/share/duanxin.png',
						name: 'dxfenxiang'
					},
					{
						text: '作废',
						icon: '/static/share/zuofei.png',
						name: 'shanchu'
					},
					{
						text: '下载',
						icon: '/static/share/xiazai.png',
						name: 'shanchu'
					}
				],
				// 默认初始底部menu数据
				bottomData: bottomShareData,
				bottomDataCopy: [{
						text: '复制',
						icon: '/static/share/copybill.png',
						name: 'shanchu'
					},

					{
						text: '打印',
						icon: '/static/share/dayin.png',
						name: 'dayin'
					},
					{
						text: '微信分享',
						icon: '/static/share/wx.png',
						name: 'wxfenxiang'
					},
					{
						text: '短信分享',
						icon: '/static/share/duanxin.png',
						name: 'dxfenxiang'
					},
					{
						text: '作废',
						icon: '/static/share/zuofei.png',
						name: 'shanchu'
					},
					{
						text: '下载',
						icon: '/static/share/xiazai.png',
						name: 'shanchu'
					},
					{
						text: '差异确认',
						icon: '/static/share/cyqr.png',
						name: 'shanchu'
					}
				],
				bottomDataByOne: [{
					text: '复制',
					icon: '/static/share/copybill.png',
					name: 'shanchu'
				}],
				// 赊账状态使用
				bottomDataByTwo: [{
						text: '微信分享',
						icon: '/static/share/wx.png',
						name: 'wxfenxiang'
					},
					{
						text: '短信签收',
						icon: '/static/share/duanxin.png',
						name: 'dxfenxiang'
					}
				],
				qrcode: '',
				Sign: '',
				type: '',
				xpqrcode: '',
				ReceiptPhrase: '', //小票短语
				billObj: {},
				// 是否可以编辑
				isEdit: false,
				// 获取小票相关信息后返回的票据id
				maindataId: "",
				// 单据状态
				mainState: false,
				// 用于微信签收、分享使用的最新单据id  保证分享后都是分享当前单据的信息
				newShareId: ""
			}
		},

		onShow() {},
		onLoad(options) {
			var that = this
			//设置是否为作废单据
			if (options.isCancel == 'true') {
				this.tagState = true
			} else {
				this.tagState = false
			}

			// 如果存在isBtnState  说明是从小票订单页跳转过来的   赋值 继续编辑条件
			if (options.isBtnState) {
				this.btnState = false
			} else {
				this.btnState = true
			}

			this.ReceiptPhrase = uni.getStorageSync('ReceiptPhrase')
			this.iprint = options.print
			this.Id = options.Id
			this.billId = options.billId


			if (options.type) this.type = options.type
			// 获取小票单据详情
			this.getTheData(this.Id, options.print)

		},
		// 监听原生导航栏的返回按钮  uniapp
		onNavigationBarButtonTap() {
			var that = this
			// var newBottomDataShezhangAll = bottomDataShezhangAll
			// 判断是否小票数据已加载完毕   再进行以下逻辑
			if (this.maindataId === "") {
				uni.showToast({
					title: '数据加载中，请稍候...',
					icon: "none"
				})
				return
			}

			console.log('可以打开弹窗了', this.maindataId, this.bottomData)

			// 现收条件的代签收 ，已签收
			if (this.entity.OrderType == 0) { //从卖家下单  可以编辑和复制 作废
				console.log('订单类型', this.entity.OrderType)
				if (this.entity.BillState == null) { //null  不需要签收  显示全部  微信分享  短信分享
					console.log('this.entity.BillState == null', this.entity.OrderType, this.entity.BillState)
					this.bottomData = []
					this.bottomData = bottomShareData
				} else if (this.entity.BillState == 0) { //待签签收   微信签收 短信签收 显示全部
					console.log('this.entity.BillState == 0', this.entity.OrderType, this.entity.BillState)
					this.bottomData = []
					this.bottomData = bottomSinDataQian
				} else if (this.entity.BillState == 1) { //1  已签收   微信签收  短信签收  不显示编辑和作废
					console.log('this.entity.BillState == 1', this.entity.OrderType, this.entity.BillState)
					this.bottomData = []
					this.bottomData = bottomNoneEditTagData
				} else if (this.entity.BillState == 2) { //拒收  不显示编辑
					console.log('this.entity.BillState == 2', this.entity.OrderType, this.entity.BillState)
					this.bottomData = []
					this.bottomData = bottomDataThreeNoneTag
				} else if (this.entity.BillState == 3) { //有差异  不显示编辑其余都显示
					console.log('this.entity.BillState == 3', this.entity.OrderType, this.entity.BillState)
					if (this.entity.Tag === '已确认') {
						console.log('已确认')
						// 说明当前差异已确认  隐藏底部差异按钮区域
						// 不显示编辑  不显示作废   显示分享
						this.bottomData = []
						this.bottomData = bottomDataThreeNoneTag
					} else {
						console.log('差异确认')
						// 说明当前差异已确认  显示底部差异按钮区域
						// 不显示编辑  不显示作废   显示分享  
						this.bottomData = []
						this.bottomData = bottomDataThree
					}
				} else if (this.entity.BillState == 4) { //作废  只显示复制
					console.log('this.entity.BillState == 4', this.entity.OrderType, this.entity.BillState)
					this.bottomData = []
					// 只显示复制底部menu
					this.bottomData = bottomCopyData
				}
			} else { //从买家下单 不允许修改买家下单的数据  隐藏编辑   复制   作废   
				if (this.entity.BillState == 1) { //1  已签收   微信签收  短信签收  不显示编辑和zuofei
					console.log('this.entity.BillState == 1', this.entity.OrderType, this.entity.BillState)
					this.bottomData = []
					this.bottomData = bottomDataShezhangFenxiang
				} else {
					this.bottomData = []
					this.bottomData = bottomDataShezhangQian
				}
				if (this.entity.BillState == 3) { //有差异
					if (this.entity.Tag === '已确认') {
						this.bottomData = []
						this.bottomData = bottomDataShezhangFenxiang
					} else {
						this.bottomData = []
						this.bottomData = bottomDataShezhangChayi
					}
				}

			}

			// 显示底部menu弹窗
			this.$refs.share.open()
			// 取消加载
			uni.hideLoading()
		},
		onBackPress(options) {
			if (!this.type && options.from == 'backbutton') {
				// 返回首页
				uni.switchTab({
					url: '/pages/index/index'
				})

				// 清空是否显示编辑条件
				// this.btnState=true
				// uni.removeStorageSync('isBtnState')
				return true; //阻止默认返回行为
			}
		},
		methods: {
			// 字符串空格处理
			getReplaceId(data) {
				if (data) {
					return data.replace(/\s/g, '').replace(/(.{4})/g, "$1 ")
				}
			},
			setNull(index) {
				this.data[index].Price = ''
			},
			// 修改是否显示编辑的函数
			setBtnState() {
				// console.log('获取缓存中的宣召数据', uni.getStorageSync('updateOrderSub'), this.data)
			},
			add() {
				//创建修改后的订单本地缓存
				uni.setStorageSync('updateOrderMain', this.entity)
				uni.setStorageSync('updateOrderSub', this.data)
				// 跳转复制版的立即开单页面
				let _propPrint = ''
				if (this.iprint) {
					_propPrint = this.iprint
				}
				uni.navigateTo({
					url: '/pages/webview/webviewOrder?Id=' + this.Id + '&billId=' + this.billId +
						'&print=' +
						_propPrint
				})
				// 存储是否从这里跳转过去   后续判断是否显示编辑条件
				// uni.setStorageSync('isBtnState', false)
			},
			sumVal() {
				let q = 0
				let p = 0
				this.data.forEach(function(itm, index) {
					q += parseInt(itm.Quantity)
					p += itm.Money
					console.log(itm)
				});
				if (this.entity.Type == 0) {
					this.entity.ReceiveSum = p
				} else {
					this.entity.PendingSum = p
				}
				this.entity.TotalQuantity = q
				this.entity.TotalSum = p
			},
			changeVal(item) {
				if (item.Price == '') {
					item.Price = 0
				}
				item.Money = item.Quantity * item.Price
				this.sumVal()
				// console.log('存储', this.entity, this.data)
				//创建修改后的订单本地缓存
				uni.setStorageSync('updateOrderMain', this.entity)
				uni.setStorageSync('updateOrderSub', this.data)
			},
			// 取消按钮
			cancel() {
				//清空订单本地缓存
				uni.removeStorageSync('updateOrderMain')
				uni.removeStorageSync('updateOrderSub')
				this.btnState = true
				this.tagState = false
				// uni.removeStorageSync('isBtnState')
				// this.getTheData(this.Id, this.iprint)
			},


			submit() {
				if (this.data.length == 0) {
					uni.showToast({
						icon: 'none',
						title: '请添加商品！',
						duration: 2000
					});
					return
				}
				let chknull = true
				this.data.forEach(item => {
					if (item.Quantity == '' && item.Price == '') {
						chknull = false
						return
					}
				})

				if (!chknull) {
					uni.showToast({
						icon: 'none',
						title: '请设置数量！',
						duration: 2000
					});
					return
				}
				let jsonStr = {
					Data: {
						maindata: this.entity,
						detalist: this.data
					}
				}
				// console.log('解析json', jsonStr)

				// 清空购物车商品  否则下次再从首页进入到立即开单就会有之前下票开单页面留下的缓存cartListData 数据
				this.$store.commit('setCartListData', [])
				// 开始修改价格
				this.$http('post', '/BO/BIlling/UpadateBIllingData', {
						maindata: this.entity,
						detalist: this.data
					})
					.then(res => {
						if (res.Success) {
							// console.log('是否修改成功是否修改成功是否修改成功', res.Data)
							this.$api.msg('修改成功')
							this.btnState = true
							// uni.removeStorageSync('isBtnState')
							if (res.Data !== "" || res.Data !== null) {
								// console.log('billstate是否=0', this.entity, this.entity.BillState, res.Data)
								// console.log('开始调用修改回执单了')
								// 同步修改回执单
								if (this.entity.BillState !== null) {
									this.getAppDataByLocation(res.Data)
								}
							}
						} else {
							this.$api.msg(res.Msg)
						}
					})


				//清空订单本地缓存
				uni.removeStorageSync('updateOrderMain')
				uni.removeStorageSync('updateOrderSub')

				// 清空是否显示编辑
				this.btnState = true
			},

			// 新增回执单

			getAppDataAddByLocation(Billid) {
				uni.showLoading({
					title: '正在生成回执单..'
				})
				// 为修改字段
				this.data.forEach(item => {
					// 商品名称
					item.Name = item.InventName
					// 总额
					item.SumiInvRCost = item.Money
					// 重量
					item.Weight = 0
					// 总重
					item.Weight1 = 0
					// 单价
					item.iInvRCost = item.Price

				})
				// 最终传递的回执单参数
				let byLocationData = {
					Billid: Billid,
					CustomerId: this.entity.CustomerId,
					Company: this.entity.CompanyId,
					DomainName: "hzq",
					Content: JSON.stringify({
						BillId: Billid,
						Customer: this.entity.cCusName,
						Subs: JSON.stringify(this.data)
					})
				}

				// console.log('最终调用的回执单修改参数是', this.data)
				this.$http('post', '/BO/BillList/AppDataByLocation', byLocationData).then(res => {
					if (res.Success) {
						console.log('新增回执单是否已生成', res)
						this.newShareId = res.Data
						// 重新获取小票相关数据
						this.getTheData(Billid, this.iprint)
						setTimeout(function() {
							uni.hideLoading()
						}, 10000)
					} else {
						that.$api.msg(res.Msg)
					}
				})
			},


			// 同时修改回执单

			getAppDataByLocation(Billid) {
				// console.log('传递的Billid', Billid)
				// 为修改字段
				this.data.forEach(item => {
					// 商品名称
					item.Name = item.InventName
					// 总额
					item.SumiInvRCost = item.Money
					// 重量
					item.Weight = 0
					// 总重
					item.Weight1 = 0
					// 单价
					item.iInvRCost = item.Price

				})
				// 最终传递的回执单参数
				let byLocationData = {
					Billid: Billid,
					CustomerId: this.entity.CustomerId,
					Company: this.entity.CompanyId,
					Content: JSON.stringify({
						BillId: Billid,
						Customer: this.entity.cCusName,
						Subs: JSON.stringify(this.data)
					})
				}

				// console.log('最终调用的回执单修改参数是', this.data)
				this.$http('post', '/BO/BillList/AppUpdateLocation', byLocationData).then(res => {
					if (res.Success) {
						// console.log('回执单是否已生成', res)

					} else {
						that.$api.msg(res.Msg)
					}
				})
			},




			del(index) {
				let that = this
				// 按钮禁用中禁止删除
				if (this.disabled == true) return
				uni.showModal({
					title: '提示',
					content: '您确定要删除此商品吗?',
					success: function(res) {
						if (res.confirm) {
							that.data.splice(index, 1)
							that.sumVal()
						}
					}
				});
			},
			// 下载
			downLoad() {
				// console.log('下载了', this.entity)
				var that = this
				uni.showLoading({
					title: '下载中'
				})
				this.$http('post', '/BO/BIlling/CreateReceipt?Id=' + this.entity.Id)
					.then(res => {
						if (res.Success) {
							// console.log('是否下载成功了', res)
							if (res.Success) {
								if (res.Data) {
									// 下载pdf
									uni.downloadFile({
										url: res.Data,
										success: function(res) {
											uni.hideLoading()
											uni.showToast({
												title: '下载成功',
											})
											const tempFilePath = res.tempFilePath;
											// console.log('下载出来了', res)
											if (res.errMsg == 'downloadFile:ok') {
												// 取消提示弹框
												// uni.hideLoading()
												uni.openDocument({
													filePath: tempFilePath,
													showMenu: true, //控制打开的word文档是否可以转发及分享
													success: function(file) {
														// console.log('file-success', file);
													},
													fail: function(err) {
														// console.log('oepndcumnet失败' + err
														// .errMsg, )
													}
												});
											}
										},
										fail: function(err) {
											that.$.toast('下载文件失败' + err.errMsg, )
										}
									});
								} else {
									// 取消提示弹框
									uni.hideLoading()
								}

							}
						} else {
							this.$api.msg(res.Msg)
						}
					})

			},
			selectShare(e) {
				// console.log('点击的是', e)
				if (e.item.text == '打印') {
					console.log('匹配打印', e.item.text)
					// 点击的是打印
					this.$refs.bleConnect.startSearch()
				} else if (e.item.name == 'wxfenxiang') {
					console.log('匹配weixinfenxiang', e.item.name)
					// 点击的是微信分享
					let title = uni.getStorageSync('CompanyInfo').ReceiptTitle
					let summary = '您在' + this.entity.CompanyName + '的购物电子小票,请点击查看'
					title = title ? title : '电子小票'
					if (this.entity.Type == 0) summary = summary + '后进行签收'


					console.log('获取当前是否最新的单据id', this.entity, this.entity.Id, this.billId)
					uni.share({
						provider: "weixin",
						scene: "WXSceneSession",
						type: 0,
						title, // 分享标题
						summary,
						href: 'https://shq.yoojet.com/#/App/Receipt?Id=' + this.entity.Id + '&billId=' + (this
							.newShareId === '' ? this.billId : this.newShareId),
						imageUrl: "https://hzq.yoojet.com/wx.png", //分享封面图片
						success: function(res) {
							// 此处是调起微信分享成功的回调
							// 分享成功后清空最新的当前单据id   好用于下次再次符合判断条件使用
							this.newShareId = ""
						},
						fail: function(err) {
							// 此处是调起微信分享失败的回调
							// console.log(err);
							this.newShareId = ""
						}
					});

					// if (!this.qrcode) return this.$api.msg('当前订单无法分享')
					// title = title ? title : '货品签收'
					// uni.share({
					// 	provider: "weixin",
					// 	scene: "WXSceneSession",
					// 	type: 5,
					// 	title: title, // 分享标题
					// 	imageUrl: "https://hzq.yoojet.com/wx.png", //分享封面图片
					// 	miniProgram: {
					// 		id: 'gh_81c3d38d48ca',
					// 		path: 'pages/index/index?q=' + encodeURIComponent(this.qrcode),
					// 		type: '0',
					// 		webUrl: 'https://hzq.yoojet.com'
					// 	},
					// 	success: function(res) {
					// 		// 此处是调起微信分享成功的回调
					// 	},
					// 	fail: function(err) {
					// 		// 此处是调起微信分享失败的回调
					// 		console.log(err);
					// 	}
					// });
				} else if (e.item.name == 'dxfenxiang') {
					console.log('匹配dxfenxiang', e.item.name)
					// 点击的是短信分享
					// if (!this.qrcode) return this.$api.msg('当前订单无法分享')
					this.$refs.popup.open()
				} else if (e.item.text == '作废') {
					console.log('匹配到作废这里了', e.item.text, this.isEdit, this.billObj.Status, this.entity.Type)
					// 点击的是作废 1  已签收  0  待签收  null 或空   不需要签收  2拒绝签收  3  有差异  4 作废 
					if (this.billObj.Status == 1 || this.billObj.Status == 2 || this.billObj
						.Status == 3 || this.billObj.Status == 4 || this.entity.Type == 0) {
						console.log('匹配当前订单无法作废')
						this.$api.msg('当前单据无法作废')
						return
					}

					// console.log('可以作废了', this.entity, this.Id)
					uni.showModal({
						title: '提示',
						content: '此操作将作废本条单据,确认继续吗?',
						success: (val) => {
							if (val.confirm) {
								uni.showLoading({
									title: '单据作废中'
								})

								this.$http('post', '/BO/BIlling/CancelBillingData?Id=' + this.entity
									.Id).then(
									res => {
										if (!res.Success) return this.$api.msg(res.Msg)
										this.$api.msg('操作成功')
										setTimeout(() => {
											uni.switchTab({
												url: '/pages/index/index'
											})
											uni.showLoading()

										}, 1000)
									})
							}
						}
					})
				} else if (e.item.text == '编辑') {
					// 点击的是编辑
					this.btnState = true
					var btnState = null
					this.btnState = false
					// 判断是否显示编辑
					this.getOrderValidation()

				} else if (e.item.text === '下载') {
					//点击的是下载
					this.downLoad()
				} else if (e.item.text === '差异确认') {
					// 点击的是差异确认

					console.log('差异确认了', this.bottomData, this.entity)
					let IP = ""
					this.getConfigSubmit(this.entity.BillListId, IP)




				} else {
					// // 复制的时候先禁用确认按钮 避免后续没有加载完点击出现问题
					// this.disabled=true
					// 点击的是复制
					// console.log('判断是否事作废数据', this.bottomData, this.tagState)
					// 判断是否事作废单据  如果事作废单据  更新底部menu为最新的menu  bottomData
					if (this.tagState == true) {
						// 说明当前单据已经事作废数据了。。更新底部menu
						let bottomData = this.newBottomData
						this.bottomData = bottomData
					}

					// 复制
					uni.showLoading({
						title: '复制中'
					})

					this.$http('post', '/BO/BIlling/CopyBIllingData', {
							detalist: this.data,
							maindata: this.entity
						})
						.then(res => {
							if (res.Success) {
								uni.hideLoading()
								// console.log('复制成功后是否调用回执单', this.entity, this.entity.BillState)
								// 判断签收状态  是否》=0  调用 回执单
								// 可以继续编辑
								this.btnState = false
								this.tagState = false
								this.$api.msg('复制成功')
								console.log('是否最终复制成功了', res, this.entity)
								if (this.entity.BillState !== null) { //不等于null就是需要代签收的状态  需要调用回执单
									console.log('走的是不生成回执单逻辑1111111111111111111111111111111', this.entity.BillState)
									// this.disabled = false

									this.getAppDataAddByLocation(res.Data)
								} else {
									// 重新获取小票相关数据
									this.getTheData(res.Data, this.iprint)
								}
							} else {
								this.$api.msg(res.Msg)
							}
						})
				}
			},

			// 差异确认
			getConfigSubmit(BillListId, IP) {
				var that = this
				uni.showModal({
					title: '温馨提示',
					content: '此操作将同意当前回执单差异,确认继续吗?',
					showCancel: true,
					success: function(res) {
						if (res.confirm) {

							let bottomData = that.bottomData
							// let index= bottomData.findIndex(item=>item.text==='差异确认')
							// 说明当前差异已确认  隐藏底部差异按钮区域
							let newBottomData = bottomData.filter((item, index) => {
								return item.text !== '差异确认'
							})
							// 重新赋值底部按钮区域
							that.bottomData = newBottomData
							uni.showLoading({
								title: '差异确认中'
							})
							that.$http('post', '/BO/BillList/ConfigSubmit?Id=' + BillListId).then(result => {
								if (result.Success) {
									uni.hideLoading()
									uni.showToast({
										title: '差异已确认', //显示的文字
										duration: 1000, //显示多少毫秒，默认1500毫秒，时间到自动隐藏
										icon: "success" //自定义显示的图标，默认成功success，错误error,加载loading，不显示图标是none
									})

									// 从新获取当前单据   显示已确认差异
									that.getTheData(that.Id, that.print)
								} else {
									that.$api.msg(result.Msg)
								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});

			},

			// 判断是否显示编辑
			getOrderValidation() {
				this.$http('post', '/BO/BIlling/OrderValidation?Id=' + this.maindataId)
					.then(res => {
						if (res.Success) {
							// console.log('是否编辑', res.Data.state)
							this.isEdit = res.Data.state

						} else {
							this.$api.msg(res.Msg)
						}
					})

			},

			//短信发送
			send(phone, str, cName) {
				this.$http('post', '/BO/BIllcollection/SendElectronicReceipt', {
					"CompanyName": cName,
					"ReceiptId": str,
					"Phone": phone
				}).then(res => {
					if (res.Success) this.$api.msg('短信发送成功')
					else this.$api.msg(res.Msg)
				})
			},
			// 短信分享
			confirm(e) {
				// console.log(e);
				if (!regPhone.test(e)) return this.$api.msg('请输入正确的手机号')
				// 请求
				console.log(this.billList)
				if (this.billList.length > 0) {
					this.billList[0].CustomerPhone = e;
					this.$http('post', '​/BO/BillList/BatchReminBill?DomainName=shq', this.billList).then(res => {
						if (res.Success) this.$api.msg('短信发送成功')
						else this.$api.msg(res.Msg)
					})
				} else {
					this.send(e, this.entity.Id, this.entity.CompanyName)
				}

				this.$refs.popup.close()
			},
			close() {
				this.$refs.popup.close()
			},

			// 小票短语
			ReceiptPhraseClose() {
				this.$refs.ReceiptPhrasePopup.close()
			},
			ReceiptPhraseConfirm(e) {
				uni.setStorageSync('ReceiptPhrase', e)
				this.$refs.bleConnect.startSearch()
				this.$refs.ReceiptPhrasePopup.close()
			},

			// 获取回执单数据
			getBillDetail() {
				console.log('获取回执但id', this.billId)
				uni.showLoading({
					title: '正在获取回执单,请稍后'
				})
				this.$http('post', '/BO/BillList/GetBillDetail', {
					id: this.billId
				}).then(res => {
					if (res.Success) {
						if (res.Data == null) return
						this.qrcode = res.Data.QrCode
						this.Sign = res.Data.Sign
						this.billObj = res.Data
						uni.hideLoading()
						this.isGet = "正在获取回执单"
					} else {
						uni.hideLoading()
						this.isGet = ""
					}
				})
			},
			// 获取小票列表数据和单号相关信息
			getTheData(Id, print) {
				var that = this
				uni.showLoading({
					title: '加载中'
				})
				this.$http('post', "/BO/BIlling/GetBllingDetail?Id=" + Id).then((res) => {
					// console.log(res);
					if (res.Success) {
						console.log('获取小票数据', res.Data)
						this.maindataId = res.Data.maindata.Id
						this.mainState = res.Data.maindata.BillState == 3 ? true : false
						if (res.Data.maindata.BillListId) {
							this.$http('post', '​/BO/BillList/GetBillFirtById?Id=' + res.Data.maindata.BillListId)
								.then(res => {
									this.billList = [res.Data];
								})
						} else {
							this.$http('post', '/BO/BIlling/OrderValidation?Id=' + res.Data.maindata.Id)
								.then(res => {
									if (res.Success) {
										// console.log('是否编辑', res.Data.state)
										this.isEdit = res.Data.state

									} else {
										this.$api.msg(res.Msg)
									}
								})
						}


						if (uni.getStorageSync('updateOrderMain') == '') {
							this.data = res.Data.detalist;
							this.entity = res.Data.maindata;
							// console.log('不等于空', res.Data)
						} else {
							this.data = uni.getStorageSync('updateOrderSub')
							this.entity = uni.getStorageSync('updateOrderMain')
							this.sumVal()
						}
						if (this.data.length > 0) {
							let TotalQuantity = 0
							let TotalSum = 0
							this.data.forEach(item => {
								this.$set(item, 'Money', Number(item.Quantity) * Number(item.Price))
								TotalQuantity = TotalQuantity + Number(item.Quantity)
								TotalSum = TotalSum + Number(item.Money)
							})
							this.entity.TotalQuantity = TotalQuantity
							this.entity.TotalSum = TotalSum
						}
						// 判断返回的单据中是否是差异已确认状态
						let bottomData = this.bottomData
						if (this.entity.Tag === '已确认') {
							// 说明当前差异已确认  隐藏底部差异按钮区域
							this.bottomData = bottomDataThreeNoneTag
						}
						this.disabled = false
						// 显示二维码区域
						// this.xpqrcode = 'https://shq.yoojet.com/#/App/Receipt?Id=' + this.Id
						var _userinfo = uni.getStorageSync('userInfo')
						this.xpqrcode = _userinfo.WeChatPay
						console.log('<<<<<'+this.xpqrcode)
						setTimeout(() => {
							console.log(this.$refs.qrcode);
							that.$refs.qrcode._makeCode()
						}, 1000)
						// 获取回执单数据
						this.getBillDetail()

						// 取消加载
						uni.hideLoading()
					} else this.$message.error(res.Msg);
				});


				// console.log(this.entity);
				// 现收散客，小票分享的时，按钮的名字要改为分享，而不是签收
				if ((this.entity.Type == 0 && this.entity.cCusName == null) || this.entity.BillState ==
					null) {
					this.bottomData[3].text = "微信分享"
					this.bottomData[4].text = "短信分享"
				}


				// 商品结算进入 直接打印
				if (print) {
					// console.log(this.entity);
					setTimeout(() => {
						this.$refs.bleConnect.startSearch()
					}, 1500)
				}

			},
			momenyFormat(num) {
				return momenyFormat(num)
			},
		}
	}
</script>

<style lang="scss" scoped>
	.tag {
		position: absolute;
		right: 10px;
		top: 10px;
		border: 1px solid red;
		height: 50px;
		width: 50px;
		line-height: 50px;
		text-align: center;
		border-radius: 50%;
		color: red;
		transform: rotate(-20deg);
	}

	.subscript {
		text-align: right;
		margin-bottom: 20rpx;
	}

	.edit_view {
		overflow: hidden;
	}

	.btn_view {
		overflow: hidden;
		position: fixed;
		left: 0;
		right: 0;
		bottom: 0;
		height: 60px;
		border-top: 1px solid #ccc;
		background: #fff;
		z-index: 9999;
	}

	.btn_edit {
		float: right;
		margin-top: 7px;
		margin-left: 8px;
		margin-right: 8px;
		width: 100px;
	}

	.btn_submit {
		float: right;
		width: 100px;
		margin-top: 7px;
		background-color: #438FE8;
	}

	.btn_add {
		margin-bottom: 4px;
		height: 30px;
		font-size: 16px;
		line-height: 30px;
	}

	.Receipt {
		.imgBox {
			display: flex;
			align-items: center;
			justify-content: center;
			height: 180px;
			overflow: hidden;
			width: 100%;
			background-color: #fff;

			.img {
				transform: rotate(-90deg);
				width: 160px;
				background-color: #fff;
			}
		}

		.txt {
			margin-top: 10px;
		}

		.container {
			margin: 0 auto;
			color: #000;
			padding: 10px 40rpx;

			.h3 {
				text-align: center;
				font-weight: bold;
			}

			.cell {
				display: flex;
				line-height: 2;

				span {
					width: 40px;
				}
			}

			.tableRow {
				font-size: 12px;
				display: flex;
				align-items: center;

				span {
					overflow: hidden;
				}

				.tableCol {
					// flex: 1;
					width: 75px;
					text-align: right;
					overflow: hidden;
				}

				.tableInp {
					font-size: 12px;
					width: 75px;
					text-align: right;
					overflow: hidden;
					border: 1px solid #ccc;
					padding: 4px;
					border-radius: 4px;
				}

				.left {
					text-align: left;
					flex: 1;
				}

				.tableIndex {
					width: 25px;
				}

				.col2 {
					width: 150px;
				}
			}
		}
	}
</style>